package com.icezzz.db_demo;

import java.math.RoundingMode;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.sql.DataSource;

import com.icexxx.iceword.IceWordUtil;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.sql.SqlExecutor;

import static com.icezzz.db_demo.StringUtils.wrap;

public class DatabaseDemo {
    public static void main(String[] args) {
        DataSource dataSource = DSFactory.get();
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        List<String> sqls = new ArrayList<>();
        final String word4096 = IceWordUtil.word4096();
        for (int i = 0; i < 10; i++) {
            String familyName = IceWordUtil.randomFamilyName();
            int length = word4096.length();
            char randomChar1 = RandomUtil.randomChar(word4096);
            char randomChar2 = RandomUtil.randomChar(word4096);
            String id = String.valueOf(1300 + i);
            String str = RandomUtil.randomString(8);
            String name = familyName + randomChar1 + randomChar2;
            String email = RandomUtil.randomString(6) + "@sina.com";
            int num = RandomUtil.randomInt(0, 9999);
            long longNum = RandomUtil.randomLong();
            String randomNumbers = RandomUtil.randomNumbers(8);
            int min = -3600 * 24 * 1000;
            int max = 3600 * 24 * 1000;
            Date date = RandomUtil.randomDate(new Date(), DateField.SECOND, min, max);
            String dateStr = DateUtil.formatDateTime(date);
            double dou = RandomUtil.randomDouble(0, 1000, 2, RoundingMode.HALF_UP);
            String ip = NetUtil.longToIpv4(longNum);
            String sql = "insert into sys_user(id,username,`password`,nickname,age,last_login_ip,last_login_time,is_delete,regist_time) values"
                    + "($id,$username,$password,$nickname,$age,$last_login_ip,$last_login_time,$is_delete,$regist_time)";
            sql = sql.replace("$id", id);
            sql = sql.replace("$username", wrap(str));
            sql = sql.replace("$password", wrap(str));
            sql = sql.replace("$nickname", wrap(name));
            sql = sql.replace("$age", num + "");
            sql = sql.replace("$last_login_ip", wrap(ip));
            sql = sql.replace("$last_login_time", wrap(dateStr));
            sql = sql.replace("$is_delete", 1 + "");
            sql = sql.replace("$regist_time", wrap(dateStr));
            sqls.add(sql);
            System.out.println(sql);
        }
        try {
            SqlExecutor.executeBatch(connection, sqls);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
